import WxValidate from '../../utils/wxValidate.js'
const app = getApp()
const locale=require(`../..//locale/${app.globalData.language}`)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    locale:locale,
    register:locale.register,
    date:'',
    region: [''],
    myInfo:null,
    salaryValue:0,
    sexs: [
      { name: locale.register.male, value: locale.register.male, checked: !1},
      { name: locale.register.female, value: locale.register.female}, 
    ],
    boards:[
      { name: locale.register.isBoard, value: locale.register.isBoard, checked: !1},
      { name: locale.register.isNotBoard, value: locale.register.isNotBoard},
    ],
    education: ['美国', '中国', '巴西', '日本'],
    industry:['教师','工人','技工'],
    marry:['未婚', '已婚','离异'],
    children:[0,1,2,3]
    
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.initValidate()
    console.log(this.WxValidate)
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  
  initValidate(){
    const rules={
      userName:{
        required:true,
      },
      phone:{
        required:true,
        tel:true
      },
      gender:{
        required:true
      },
      birthDate:{
        required:true
      },
      liveCity:{
        required:true,
        value:true
       },
      board:{
        required:true
      },
      education:{
        required:true
      },
      industry:{
        required:true
      },
      salary:{
        required:true
      },
      marry:{
        required:true
      },
      children:{
        required:true
      }
    }

    const messages={
      userName: {
        required: '请输入姓名',
      },
      phone: {
        required: '请输入手机号码',
        tel: '请输入正确的手机号'
      },
      gender: {
        required: '请选择您的性别'
      },
      birthDate: {
        required: '请选择出生年份'
      },
      liveCity: {
        required: '请选择居住城市'
      },
      board: {
        required: "请选择是否曾在海外居住"
      },
      education: {
        required: '请选择教育水平'
      },
      industry: {
        required: '请选择从事行业'
      },
      salary: {
        required: '请选择月收入'
      },
      marry: {
        required: '请选中婚姻状况'
      },
      children: {
        required: '请选择子女数量'
      }
    }
    this.WxValidate=new WxValidate(rules,messages)
    this.WxValidate.addMethod('value', (value, param) => {
      return this.WxValidate.optional(value) || (value.length > 1)
    }, '请选择居住城市')
  },


//sexs
  bindSexsChange:function(e){
     let value=e.detail.value;
      let sexs = this.data.sexs
      const items = sexs.map(n=>{
        return Object.assign({},n,{
          checked:n.value===value
        })
      })
      this.setData({
        sexs:items
      })
      
  },
 
  bindBoardChange:function(e){
    let value = e.detail.value;
    let boards = this.data.boards
    const items = boards.map(n => {
      return Object.assign({}, n, {
        checked: n.value === value
      })
    })
    this.setData({
      boards: items
    })

  },

  bindDateChange: function (e) {
      console.log('出生日期', e.detail.value)
      this.setData({
        date: e.detail.value
      })
  },

  bindRegionChange: function (e) {
      console.log('居住城市', e.detail.value)
      this.setData({
        region: e.detail.value
      })
  },



  EducationChange:function(e){
      this.setData({
        index: e.detail.value
      })
  },

  IndustryChange:function(e){
      this.setData({
        key: e.detail.value
      })
  },

  Salarychange:function(e){
      console.log('工资', e.detail.value)
      this.setData({
        salaryValue:e.detail.value * 100
      })

  },

  MarryChange:function(e){
      this.setData({
        keyMarry: e.detail.value
      })
  },

  ChildrenChange:function(e){
      this.setData({
        keyChildren: e.detail.value
      })
  },

formSubmit: function (e) {
      console.log(e.detail.value)
      let data=e.detail.value;
      if (!this.WxValidate.checkForm(data)){
          const error = this.WxValidate.errorList[0]
          this.showTips(error.msg)
          return false
      }
         console.log("success")
         wx.navigateTo({
            url: '/pages/success/success'
         })
      //  if(this.isEmpty(data.username)){
      //       this.showTips('姓名不能为空')
      //       return false
      //  } else if (!this.isMobile(data.phone)){
      //    this.showTips('请填写正确的手机号')
      //    return false
      //  } else if (!data.birthDate){
      //    this.showTips('请选择出生年份')
      //    return false
      //  } else if (this.isEmpty(data.liveCity)){
      //    this.showTips("请选择居住城市")
      //    return false
      //  }
      //   else if (this.isEmpty(data.education)){
      //    this.showTips('请选择教育水平')
      //    return false
      //  }else if(!data.industry){
      //    this.showTips('请选择从事行业')
      //    return false
      //  }else if(this.isEmpty(data.salary)){
      //    this.showTips('请选择月收入')
      //    return false
      //  }else if(!data.marry){
      //    this.showTips('请选择婚姻状况')
      //    return false
      //  }else if(!data.children){
      //    this.showTips('请选择子女数量')
      //    return false
      //  }else{
      //    wx.navigateTo({
      //      url: '/pages/success/success'
      //    })
      //  }
          

    },

  isEmpty:function(value){
        let rule = /^[\s\S]*.*[^\s][\s\S]*$/;
        if(value==null){
          return true
        }else{
          return !rule.test(value)
        }
      
  },

  isMobile:function(value){
      let rule = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
      return rule.test(value)
  },

  showTips:function(text){
      wx.showModal({
        content: text,
        showCancel: false,
      })
  }


})

  